package com.atguigu;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import static org.apache.hadoop.hbase.KeyValue.Type.Put;

public class WriteHbaseExample {
    private static final byte[] CF = Bytes.toBytes("cf"); // column family
    private static final byte[] QUALIFIER = Bytes.toBytes("qualifier"); // column qualifier
    private static final int VERSION = 1; // version number
    public static void writeToHBase(String tableName, String rowKey, String value) throws IOException {
        Configuration config = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(config);
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(CF, QUALIFIER, VERSION, Bytes.toBytes(value));
        table.put(put);
        table.close();
        connection.close();
    }
}
